package cn.wsn2018.tools.util;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.dao.LogsInfoDao;
import cn.wsn2018.tools.model.Log.ErrorLogInfo;
import cn.wsn2018.tools.model.Log.LogsInfo;
import cn.wsn2018.tools.model.Log.WarningLogInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;


/**
 * Created by NZX on 2018/12/19.
 */
@Component
public class LogUtil {

    private static Logger logger = LoggerFactory.getLogger(LogUtil.class);

    @Autowired
    private LogsInfoDao logsInfoDao;
    private static LogUtil logUtil;

    @PostConstruct
    public void init(){
        logUtil = this;
        logUtil.logsInfoDao = this.logsInfoDao;
    }

    //打印日志工具
    public static boolean produceLogs(String username,String type,String info,String result){
        if((username == null || username.isEmpty()) || (type == null || type.isEmpty()) ||
                (info == null || info.isEmpty()) || (result == null || result.isEmpty())){
            return false;
        }

        LogsInfo logsInfo = new LogsInfo();
        logsInfo.setUsername(Regex.checkReg(username));
        logsInfo.setCreateTime(new DateUtil().dateToString(new Date()));
        logsInfo.setOperateInfo(Regex.checkReg(info));
        logsInfo.setResult(Regex.checkReg(result));
        logsInfo.setType(Regex.checkReg(type));
        logger.info(logsInfo.toString());
        boolean flag = logUtil.logsInfoDao.saveLogsInfo(logsInfo);
        return flag;
    }

    public static boolean produceErrorLogs(ErrorLogInfo errorLogInfo){
        /**
         * @Description:
         * @Param: [errmessage]
         * @return: boolean
         * @Author: SongDaisong
         * @Date: 14:04
         */

        logger.error(errorLogInfo.toString());

        logUtil.logsInfoDao.saveLogInfo(errorLogInfo,"errorLogInfo");

        return true;
    }

    public static  boolean produceWarningLogs(String username,String warningType,String warningLevel,String warningModuleName,String ipAddress,String warningMessage){
        /**
        * @Description: 产生告警日志
        * @Param: [username, warningType, warningLevel, warningModuleName, ipAddress, warningMessage]
        * @return: boolean
        * @Author: SongDaisong
        * @Date:  2019/12/11 16:13
        */
        if(StringUtil.isEmptyString(username)){
            username="null";
        }
        WarningLogInfo warningLogInfo = new WarningLogInfo();
        warningLogInfo.setWarningType(warningType);
        warningLogInfo.setWarningLevel(warningLevel);
        warningLogInfo.setIpAddress(ipAddress);
        warningLogInfo.setWarningInfo(warningMessage);
        warningLogInfo.setType(CommonValue.LOG_TYPE_0);
        warningLogInfo.setUserName(username);
        warningLogInfo.setWarningModuleName(warningModuleName);
        warningLogInfo.setCreateTime(new DateUtil().dateToString(new Date()));
        logger.error(warningLogInfo.toString());
        return logUtil.logsInfoDao.saveLogInfo(warningLogInfo,"warningLogInfo");
    }

    public static String produceWarningMsg(String warningType,String username,String role,String moduleName){
        if(CommonValue.WARNING_TYPE_1.equals(warningType)){
            return warningType+":用户名为"+username+"的"+ role +"用户试图访问"+moduleName;
        }else{
            return warningType+":用户名为"+username+"的尝试异常登录";
        }

    }



}
